// 页面加载完成后执行
document.addEventListener('DOMContentLoaded', () => {
    loadElderlyOptions();
});

// 加载老年人选项
async function loadElderlyOptions() {
    try {
        const response = await fetch('/api/elderly');
        const data = await response.json();
        const select = document.getElementById('elderlySelect');
        
        data.forEach(elderly => {
            const option = document.createElement('option');
            option.value = elderly.id;
            option.textContent = elderly.name;
            select.appendChild(option);
        });
    } catch (error) {
        console.error('Error:', error);
    }
}

// 加载健康建议
async function loadHealthAdvice(elderlyId) {
    if (!elderlyId) {
        document.getElementById('advice-content').innerHTML = '';
        document.getElementById('current-health-status').textContent = '';
        return;
    }

    try {
        const response = await fetch(`/api/health_advice?elderly_id=${elderlyId}`);
        const data = await response.json();
        
        if (data.length === 0) {
            document.getElementById('advice-content').innerHTML = 
                '<div class="no-data">暂无健康记录，无法生成建议</div>';
            document.getElementById('current-health-status').textContent = '未知';
            return;
        }
        
        const statusClass = getHealthStatusClass(data[0].health_status);
        document.getElementById('current-health-status').className = `health-status ${statusClass}`;
        document.getElementById('current-health-status').textContent = data[0].health_status;
        
        const adviceContent = document.getElementById('advice-content');
        let html = `
            <div class="advice-list">
                <div class="advice-header">
                    <i class="ri-heart-pulse-line"></i>
                    <h3>根据最近的健康状况，为您提供以下建议：</h3>
                    <p class="advice-date">更新时间：${data[0].created_at}</p>
                </div>
        `;
        
        data.forEach(advice => {
            let typeClass = '';
            let icon = '';
            
            switch(advice.advice_type) {
                case '体温建议':
                    typeClass = 'medical';
                    icon = 'ri-thermometer-line';
                    break;
                case '血压建议':
                    typeClass = 'medical';
                    icon = 'ri-heart-pulse-line';
                    break;
                case '生活方式建议':
                    typeClass = 'lifestyle';
                    icon = 'ri-plant-line';
                    break;
                default:
                    typeClass = 'monitoring';
                    icon = 'ri-health-book-line';
            }
            
            html += `
                <div class="advice-item ${typeClass}">
                    <h4>
                        <i class="${icon}"></i>
                        ${advice.advice_type}
                    </h4>
                    <p>${advice.content}</p>
                </div>
            `;
        });
        
        html += '</div>';
        adviceContent.innerHTML = html;
    } catch (error) {
        console.error('Error:', error);
    }
}

// 获取健康状态样式类
function getHealthStatusClass(status) {
    switch(status) {
        case '非常健康': return 'status-excellent';
        case '良好': return 'status-good';
        case '及格': return 'status-fair';
        case '需要治疗': return 'status-poor';
        default: return '';
    }
} 